// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/init/init.h>
#include <pollux/buffer/buffer.h>
#include <pollux/exec/prefixsort/prefix_sort_algorithm.h>
#include <pollux/exec/prefixsort/tests/utils/EncoderTestUtils.h>

TURBO_FLAG(int32_t,sort_data_seed, 1, "random test data generate seed.");

using namespace kumo::pollux;
using namespace kumo::pollux::exec;

namespace {

class PrefixSortAlgorithmBenchmark {
 public:
  void seed(int32_t seed) {
    rng_.seed(seed);
  }

  void runQuickSort(std::vector<int64_t> vec) {
    char* start = (char*)vec.data();
    uint32_t entrySize = sizeof(int64_t);
    auto swapBuffer = AlignedBuffer::allocate<char>(entrySize, pool_.get());
    auto sortRunner =
        prefixsort::PrefixSortRunner(entrySize, swapBuffer->asMutable<char>());
    sortRunner.quickSort(
        start, start + entrySize * vec.size(), [&](char* a, char* b) {
          return memcmp(a, b, 8);
        });
  }

  std::vector<int64_t> generateTestVector(int32_t size) {
    std::vector<int64_t> randomTestVec(size);
    std::generate(randomTestVec.begin(), randomTestVec.end(), [&]() {
      return melon::Random::rand64(rng_);
    });
    prefixsort::test::encodeInPlace(randomTestVec);
    return randomTestVec;
  }

 private:
  std::shared_ptr<memory::MemoryPool> pool_{
      memory::memoryManager()->addLeafPool()};
  melon::Random::DefaultGenerator rng_;
};

std::unique_ptr<PrefixSortAlgorithmBenchmark> bm;

std::vector<int64_t> data10k;
std::vector<int64_t> data100k;
std::vector<int64_t> data1000k;
std::vector<int64_t> data10000k;

BENCHMARK(PrefixSort_algorithm_10k) {
  bm->runQuickSort(data10k);
}

BENCHMARK(PrefixSort_algorithm_100k) {
  bm->runQuickSort(data100k);
}

BENCHMARK(PrefixSort_algorithm_1000k) {
  bm->runQuickSort(data1000k);
}

BENCHMARK(PrefixSort_algorithm_10000k) {
  bm->runQuickSort(data10000k);
}

} // namespace

int main(int argc, char** argv) {
  melon::Init init(&argc, &argv);
  memory::MemoryManager::initialize({});
  bm = std::make_unique<PrefixSortAlgorithmBenchmark>();
  bm->seed(FLAGS_sort_data_seed);
  data10k = bm->generateTestVector(10'000);
  data100k = bm->generateTestVector(100'000);
  data1000k = bm->generateTestVector(1'000'000);
  data10000k = bm->generateTestVector(10'000'000);
  melon::runBenchmarks();
  return 0;
}
